<template>
  <div>
    <el-form ref="searchForm" :inline="true" :model="searchMap" class="demo-form-inline">
      <!-- 重置会看 el-form-item 组件元素的prop上是否指定了字段名 -->
      <el-form-item prop="name">
        <el-input v-model="searchMap.name" placeholder="供应商名称"></el-input>
      </el-form-item>
      <el-form-item prop="linkname" v-if="!isDialog">
        <el-input v-model="searchMap.linkname" placeholder="联系人"></el-input>
      </el-form-item>
      <el-form-item prop="mobile" v-if="!isDialog">
        <el-input v-model="searchMap.mobile" placeholder="联系电话"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="fetchData" style="margin-left:20px">查询</el-button>
        <el-button type="success" @click="handleAdd" v-if="!isDialog">新增</el-button>
        <el-button @click="resetForm('searchForm')" v-if="!isDialog">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- highlight-current-row激活单选行 
        @current-change 点击某一行后,会触发这个时间,从而调用对应的函数handleCurrentChange
        handleCurrentChange函数会接受两个参数currentRow,oldCurrentRow
    -->
    <el-table
      :data="list"
      border
      style="width: 100%"
      :highlight-current-row="false"
      @current-change="handleCurrentChange"
    >
      <el-table-column type="index" label="序号"></el-table-column>
      <el-table-column prop="name" label="供应商名称" width="180px"></el-table-column>
      <el-table-column prop="linkname" label="联系人" width="120px"></el-table-column>
      <el-table-column prop="mobile" label="联系电话" width="180px" v-if="!isDialog"></el-table-column>
      <el-table-column prop="remark" label="备注" v-if="!isDialog"></el-table-column>
      <el-table-column label="操作" width="200" v-if="!isDialog">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row.id)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        :layout="!isDialog ? 'total ,prev, pager, next, jumper': 'prev, pager, next'"
        background
        @current-change="handlePageChange"
        :current-page="currentPage"
        :page-size="pageSize"
        :total="total"
      ></el-pagination>
    </div>
    <el-dialog title="供应商编辑" :visible.sync="dialogFormVisible" width="500px" v-if="!isDialog">
      <el-form
        :rules="rules"
        :model="pojo"
        ref="pojoForm"
        label-width="100px"
        label-position="right"
        style="width:400px"
      >
        <el-form-item label="供应商名称" prop="name">
          <el-input v-model="pojo.name"></el-input>
        </el-form-item>
        <el-form-item label="联系人" prop="linkname">
          <el-input v-model="pojo.linkname"></el-input>
        </el-form-item>
        <el-form-item label="联系电话" prop="mobile">
          <el-input v-model="pojo.mobile"></el-input>
        </el-form-item>
        <el-form-item label="备注栏" prop="remark">
          <el-input type="textarea" v-model="pojo.remark"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <!-- <el-button type="primary" @click="addData('pojoForm')">确 定</el-button> -->
        <el-button
          type="primary"
          @click="pojo.id === null ? addData('pojoForm') : updateData('pojoForm')"
        >确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import supplierApi from "@/api/supplier";

export default {
  props: {
    // 接受父组件传递过来的数据,通过isDialog并判断是否为弹窗
    isDialog: Boolean
  },
  data() {
    return {
      list: [],
      pageSize: 9,
      currentPage: 1,
      total: 0,
      searchMap: {
        name: "",
        linkname: "",
        mobile: ""
      },
      dialogFormVisible: false,
      pojo: {
        id: null,
        name: "",
        linkname: "",
        mobile: "",
        remark: ""
      },
      rules: {
        name: [
          { required: true, message: "供应商名称不能为空", trigger: "blur" }
        ],
        linkname: [
          { required: true, message: "联系人不能为空", trigger: "blur" }
        ],
        mobile: [
          { required: true, message: "联系电话不能为空", trigger: "blur" }
        ],
      } // 校验规则
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    fetchData() {
      supplierApi
        .search(this.currentPage, this.pageSize, this.searchMap)
        .then(response => {
          const resp = response.data;
          this.list = resp.data.rows;
          this.total = resp.data.total;
        });
    },
    handlePageChange(page) {
      this.currentPage = page;
      this.fetchData();
    },
    // 重置会看 el-form-item 组件元素的prop上是否指定了字段名
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    handleAdd() {
      this.dialogFormVisible = true;
      this.$nextTick(() => {
        this.$refs["pojoForm"].resetFields();
      });
    },
    addData(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          // 校验通过,提交表单
          supplierApi.add(this.pojo).then(response => {
            const resp = response.data;
            if (resp.flag) {
              // 新增成功,舒心列表数据
              this.fetchData();
              // 关闭窗口
              this.dialogFormVisible = false;
              this.$message({
                message: resp.message,
                type: "success"
              });
            } else {
              this.$message({
                message: resp.message,
                type: "warning"
              });
            }
          });
        } else {
          return false;
        }
      });
    },
    handleDelete(id) {
      this.$confirm("此操作将永久删除该信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          supplierApi.deleteById(id).then(response => {
            const resp = response.data;
            this.$message({
              message: resp.message,
              type: resp.flag ? "success" : "error"
            });
            if (resp.flag) {
              this.fetchData();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    handleEdit(id) {
      this.handleAdd();
      supplierApi.getById(id).then(response => {
        const resp = response.data;
        if (resp.flag) {
          this.pojo = resp.data;
        }
      });
    },
    updateData(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          // 校验通过,提交表单
          supplierApi.update(this.pojo).then(response => {
            const resp = response.data;
            if (resp.flag) {
              // 新增成功,舒心列表数据
              this.fetchData();
              // 关闭窗口
              this.dialogFormVisible = false;
              this.$message({
                message: resp.message,
                type: "success"
              });
            } else {
              this.$message({
                message: resp.message,
                type: "warning"
              });
            }
          });
        } else {
          return false;
        }
      });
    },
    // 当点击某一行时,会调用当前函数
    handleCurrentChange(currentRow) {
      // 点击后,要将点击的数据传递到父组件(商品管理)中,则可以通过触发父组件中
      // 的option-supplier,父组件可以在option-supplier这个事件对应的处理函数中进行接受数据
      this.$emit("option-supplier", currentRow);
    },
  }
};
</script>

<style scoped>
.el-form {
  margin: 20px 0 -6px;
}
.block {
  margin: 20px 0 0 5px;
}
</style>